package com.master.echoserver;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 读操作的工具类
 *
 */
public class RequestProcessor {

    //构造线程池
    private static ExecutorService  executorService  = Executors.newFixedThreadPool(10);
	//private static ExecutorService  executorService  = Executors.newSingleThreadExecutor();
    public static void ProcessorRequest(final SelectionKey key){
        //获得线程并执行
        executorService.submit(new Runnable() {

            @Override
            public void run() {
                try {
                    System.out.println("RequestProcessor线程"+Thread.currentThread().getId()+" 处理IO读写");
                    SocketChannel requestChannel = (SocketChannel) key.channel();
                    // I/O读数据操作
                    ByteBuffer buffer = ByteBuffer.allocate(1024);
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    int len = 0;
                    while (true) {
                        buffer.clear();
                        len = requestChannel.read(buffer);
                        if (len == -1) {
                        	break;	
                        }
                        	
                        buffer.flip();
                        while (buffer.hasRemaining()) {
                            baos.write(buffer.get());
                        }
                    }
                    String requestString= new String(baos.toByteArray(),"UTF-8");
                    System.out.println("RequestProcessor线程"+Thread.currentThread().getId()+"处理服务器端接收到的数据："+ requestString);
                    
                    requestChannel.write(ByteBuffer.wrap(("RequestProcessor线程"+Thread.currentThread().getId()+"处理请求数据："+requestString).getBytes()));
                    requestChannel.shutdownOutput();
                   /*  //将数据添加到key中
                    key.attach(baos);
                    //将注册写操作添加到队列中
                    NIOServerSocket.addWriteQueen(key);
                  */

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}